import { modal } from 'vue-strap'
import DatePicker from 'vue-datepicker'
import extend from 'extend'
import getDatePickerOption from '../../../util/date-picker-option'
import { getGolds, setGolds, showMsg } from '../../../vuex/action'

const validation = {
  golds(dataArray) {
    for (let value of dataArray) {
      for (let name in value) {
        if (['title', 'duration', 'price', 'discount'].indexOf(name) > -1 && !(value[name] + '').length) {
          return false
        }
      }
    }
    return true
  }
}

export default {
  name: 'UpdatePay',
  props: ['item'],
  components: {
    'vue-modal': modal,
    'vue-date-picker': DatePicker
  },
  data() {
    return {
      showUpdateAdModal: false,
      golds: [],
      gold: {
        id: -1,
        title: '',
        duration: '',
        durationType: 1,
        price: '',
        discount: '',
        type: 0,
        newType: 0
      },
      datePickerOption: getDatePickerOption()
    }
  },
  vuex: {
    actions: {
      xGetGolds: getGolds,
      xSetGolds: setGolds,
      xShowMsg: showMsg
    }
  },
  methods: {
    addPay() {
      var gold = extend(true, {}, this.gold)
      this.golds.push(gold)
    },
    showUpdateDialog() {
      this.xGetGolds({
        betaId: this.item.betaId
      })
        .then((golds) => {
          this.golds = golds.map((it) => {
            it.type = 1
            it.newType = 1
            return it
          })
          this.showUpdateAdModal = true
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    deleteGold(gold) {
      gold.newType = 2
    },
    enableGold(gold) {
      gold.newType = gold.type
    },
    checkDuration(duration) {
      if (duration < 1) {
        this.xShowMsg({
          type: 'danger',
          content: '天数不能少于 1 天'
        })
      }
    },
    checkPrice(price) {
      if (price < 0) {
        this.xShowMsg({
          type: 'danger',
          content: '价格不能小于 0'
        })
      }
    },
    checkDiscount(discount) {
      if (discount < 0 || discount > 100) {
        this.xShowMsg({
          type: 'danger',
          content: '折扣需在 0 ~ 100 内'
        })
      }
    },
    submit() {
      var golds = this.golds.slice(0).filter((it) => {
        return it.newType !== 2
      })

      var valid = validation.golds(golds)
      if (!valid) {
        return this.xShowMsg({
          type: 'danger',
          content: '* 标记的均为必填/必选项'
        })
      }

      golds = golds.map((it) => {
        if (it.duration < 1
          || it.price < 0
          || it.discount < 0
          || it.discount > 100) {
          valid = false
        }
        return {
          id: it.id,
          title: it.title,
          duration: +it.duration,
          durationType: +it.durationType,
          price: +it.price,
          discount: +it.discount,
          type: it.newType
        }
      })

      if (!valid) {
        return this.xShowMsg({
          type: 'danger',
          content: '输入有误，请检查'
        })
      }

      this.xSetGolds({
        'betaId': this.item.betaId,
        'golds': JSON.stringify(golds)
      })
        .then(() => {
          this.showUpdateAdModal = false
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  }
}
